package com.hmall.delay;

import com.hmall.delay.model.Message;
import com.hmall.delay.model.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RedisMQ {

    /**
     * 消息池前缀，以此前缀加上传递的消息id作为key，以消息{@link Message}
     * 的消息体body作为值存储
     */
    private static final String MSG_POOL = "Message:Pool:";
    /**
     * 默认监听数量，对应监听zset队列前多少个元素
     */
    private static final int DEFAUT_MONITOR = 10;

    @Autowired
    JedisPool jedisPool;

    /**
     * 每次监听queue中元素的数量，可配置
     */
    private int monitorCount = DEFAUT_MONITOR;

    /**
     * 消息路由
     */
    private List<Route> routes;

    /**
     * 存入消息池
     * @param message
     * @return
     */
    public boolean addMsgPool(Message message) {

        if (null != message) {
            Jedis jedis = jedisPool.getResource();
            jedis.setex(MSG_POOL + message.getId(), message.getTtl(),  message.getBody());
            jedis.close();
            return true;
        }
        return false;
    }

    /**
     * 从消息池中删除消息
     * @param id
     * @return
     */
    public boolean deMsgPool(String id) {

        Jedis jedis = jedisPool.getResource();
        jedis.del(MSG_POOL + id);
        jedis.close();
        return true;
    }

    /**
     * 像队列中添加消息
     * @param key
     * @param score 优先级
     * @param val
     * @return 返回消息id
     */
    public String enMessage(String key, long score, String val) {

        try {
            Jedis jedis = jedisPool.getResource();
            jedis.zadd(key, score, val);
            jedis.close();
            return val;
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 从队列删除消息
     * @param id
     * @return
     */
    public boolean deMessage(String key, String id) {

        try {
            Jedis jedis = jedisPool.getResource();
            jedis.zrem(key, id);
            jedis.close();
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 消费
     * @return
     */
    public List<String> consume(String key) {

        Jedis jedis = jedisPool.getResource();
        long count = jedis.llen(key);
        if (0 < count) {
            // 可根据需求做限制

            List<String> idList = jedis.lrange(key, 0, count);
            if (idList != null) {
                List<String> result = new ArrayList<>();
                idList.forEach(l -> result.add(jedis.get(MSG_POOL + l)));
                for(String id : idList) {
                    jedis.lrem(key, count, id);
                }
                jedis.close();
                return result;
            } /// if end~
        }
        jedis.close();

        return null;
    }


    public int getMonitorCount() {
        return monitorCount;
    }

    public void setMonitorCount(int monitorCount) {
        this.monitorCount = monitorCount;
    }

    public List<Route> getRoutes() {
        return routes;
    }

    public void setRoutes(List<Route> routes) {
        this.routes = routes;
    }
}